<?php

namespace app\jigou\controller\qingdongams\workorder;

use addons\qingdongams\model\Consume;
use addons\qingdongams\model\CustomerProduct;
use addons\qingdongams\model\OperationLog;
use addons\qingdongams\model\StaffSignIn;
use addons\qingdongams\model\Workorder as WorkorderModel;
use addons\qingdongams\model\WorkorderLogistics;
use app\jigou\controller\qingdongams\Base;
use think\Db;

/**
 * 回访工单管理
 * @icon fa fa-user
 */
class Visit extends Base {

    protected $relationSearch = true;
    protected $searchFields   = 'id,workorder_number,title';
	/**
	 * @var \addons\qingdongams\model\Workorder
	 */
	protected $model = null;


	public function _initialize() {
		parent::_initialize();
		$this->model = new WorkorderModel();
	}


	/**
	 * 查看
	 */
	public function index() {
		//设置过滤方法
		$this->request->filter(['strip_tags', 'trim']);
		if ($this->request->isAjax()) {
			//如果发送的来源是Selectpage，则转发到Selectpage
			if ($this->request->request('keyField')) {
				return $this->selectpage();
			}
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $otherWhere=[];
            $otherWhere['status'] = 3;
            $otherWhere['workorder_type'] = ['in', WorkorderModel::getVisitType()];

            $list = $this->model->where($where)->where($otherWhere)->with([
                'customer',
                'ownerStaff',
                'contacts'])->order('id desc')->paginate($limit);

            $result = array("total" => $list->total(), "rows" => $list->items());

            return json($result);
		}

		return $this->view->fetch();
	}

	//工单详情
	public function detail($ids = null) {
	    // 基本信息
		$row = $this->model->with(['create_staff','createPerson','customer', 'owner_staff'])->where([
			'id'       => $ids,
		])->find()->toArray();
		$status=[0=> '待分配', 1 => '待处理', 2 => '处理中', 3 => '已完成'];
		$process=[0=> '已提交', 1 => '备货中', 2 => '备货完成', 3 => '到访/录入物流信息', 9 => '已送达'];
        $row['status'] = ($status[$row['status']]) ?? '';
        $row['process'] = ($process[$row['process']]) ?? '';
		$this->assign('row', $row);
		$this->assign('ids', $ids);
        // 跟进记录
        $where = [
            'relation_type'   =>  'workorder',
            'relation_id'   =>  $ids,
        ];
        $records = StaffSignIn::where($where)->with([
            'staff'
        ])->order('id desc')->select();
        $this->assign('records',$records);

        // 产品信息
        $products = CustomerProduct::where('id',$row['customer_product_id'])->find();
        $this->assign('products',$products);
        // 消费
        $consumes = Consume::where(['relation_type'=>'workorder','relation_id'=>$ids])->with('staff')->select();
        $this->assign('consumes',$consumes);
        // 物流信息
        $logistics = WorkorderLogistics::where('workorder_id',$ids)->find();
        if($logistics){
            $logistics=$logistics->toArray();
            $logistics['content']=$logistics['content']?$logistics['content']:[];
            $logistics['content']=array_reverse($logistics['content']);
            $logistics['company']=Db::name('companys')->where(['no'=>$logistics['company']])->value('com');
        }
        $this->assign('logistics',$logistics);

        // 操作记录
        $operations = collection(OperationLog::where(['relation_type'=>8,'relation_id'=>$ids])->with(['staff'])->select())->toArray();
        $this->assign('operations',$operations);
		return $this->view->fetch();
	}


}
